package com.jhws.sys.role.controller;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import com.jhws.sys.role.bo.TblRoleDimBo;
import com.jhws.sys.role.bo.TblRolemenuDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.GetId;
import com.jhws.sys.menu.service.ITblMenuDimService;
import com.jhws.sys.role.service.ITblRoleDimService;
import com.jhws.sys.role.service.ITblRolemenuDimService;
import com.jhws.sys.user.service.ITblUserDimService;

@Controller
@RequestMapping({"/RoleCtrl","/role"})
public class RoleCtrl {
	
	@Resource
	private ITblRoleDimService IRoleService;
	
	@Resource
	private ITblRolemenuDimService IRoleMenuService;
	
	@Resource
	private ITblMenuDimService IMenuService;
	
	@Resource
	private ITblUserDimService IUserService;

	@RequestMapping("save.x")
    @ResponseBody
	public JsonRespData save(TblRoleDimBo role) throws Exception {

		if(StringUtils.isEmpty(role.getRoleCode())){
			role.setRoleCode(GetId.randomID());
		}

	    if(StringUtils.isNotEmpty(role.getRoleParent())) {
            TblRoleDimBo parentRole = IRoleService.findTbleRoleByRoleName(role.getRoleParent());
            String parents = StringUtils.isNotEmpty(parentRole.getRoleParents())?parentRole.getRoleParents()+",":"";
            role.setRoleParents( parents + role.getRoleParent());
        }

		IRoleService.save(role);
		return new JsonRespData("",1,"");
	}

	@RequestMapping("remove.x")
	@ResponseBody
	public JsonRespData removeTblRoleDim(String roleCode) throws Exception {
		ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
		String message;
		if(roleCode == null)
			message = "删除失败,角色编号为空!";
		else {
			IRoleService.removeRoleDim(roleCode);
			message = "角色删除成功";
		}
		
		return new JsonRespData("",1,message);
	}

	
	@RequestMapping("saveRoleMenu.x")
    @ResponseBody
	public JsonRespData saveRoleMenu(String roleCode, String [] menuCode) throws Exception
	{
		/* 保存前必须先删掉之前该角色的所有菜单 */
		IRoleMenuService.deleteTblRolemenuDimByRoleCode(roleCode);
		for(String code : menuCode) {
			TblRolemenuDimBo bo = new TblRolemenuDimBo();
			bo.setRoleCode(roleCode);
			bo.setMenuCode(code);
			IRoleMenuService.save(bo);
		}
		return new JsonRespData("",1,"");
	}

	/**
	 * 获取所有角色列表,创建角色时选择父角色的时候使用
	 */
	@RequestMapping("getRoleList.x")
	@ResponseBody
	public JsonRespData getRoleList() throws Exception{
		return new JsonRespData("",1,IRoleService.findTblRoleDimByWhere(""));
	}

	/**
	 * 通过角色ID获取到角色数据,forSave.html使用
	 * @param roleCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("getRoleByCode.x")
	@ResponseBody
	public JsonRespData getRoleByCode(String roleCode) throws Exception{
		return new JsonRespData("",1, IRoleService.findTblRoleDimById(roleCode));
	}

	@RequestMapping("getMenuOfRole.x")
	@ResponseBody
	public JsonRespData getMenuByRole(String roleCode){
		return new JsonRespData("",1,IRoleMenuService.getMenuByRole(roleCode));
	}

}